﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;


#if Model_1
//TODO 入站数据展示类名引用
using IMData = JinYuan.Models.Loading.PreweldingEntity;
//TODO 出站数据展示类名引用
using OUData = JinYuan.Models.Product.ProductionMoveOut;

#elif Model_2
//TODO 入站数据展示类名引用
using IMData = JinYuan.Models.Loading.FMoveInEntity;
//TODO 出站数据展示类名引用
using OUData = JinYuan.Models.FinalWeldingGearEntity;

#elif Model_3
//TODO 入站数据展示类名引用
using IMData = JinYuan.Models.Loading.DMoveInEntity;
//TODO 出站数据展示类名引用
using OUData = JinYuan.Models.DiolameGearEntity;

#elif Model_4
//TODO 入站数据展示类名引用
using IMData = JinYuan.Models.Loading.PETLoadingEntity;
//TODO 出站数据展示类名引用
using OUData = JinYuan.Models.Product.DiolameMoveOut;
#else
//TODO 入站数据展示类名引用
using IMData = JinYuan.Models.AGearEntity;
//TODO 出站数据展示类名引用
using OUData = JinYuan.Models.BGearEntity;
//TODO 钢壳数据展示类名引用
using CData = JinYuan.Models.CGearEntity;
#endif

namespace LargeSquareOne.ViewModel
{
    public static class ViewModelBase
    {

        /// <summary>
        /// 上料条码电池数据显示
        /// </summary>
        public static BindingList<IMData> listTA = new BindingList<IMData>();
        /// <summary>
        /// 下料电池数据绑定委托
        /// </summary>
        /// <param name="m"></param>
        public static BindingList<OUData> listTB = new BindingList<OUData>();

        public static void initShowLowDataTable(this FrmMain frmMain)
        {

            //M_DBCtrl._MachineInSQLTable = typeof(IMData).Name;
            //M_DBCtrl._MachineOutSQLTable = typeof(OUData).Name;
        }

        #region 显示上料数据
        public static void ShowFeedingData(this FrmDataMonitor from, List<object> list)
        {
            try
            {
                if (from.dgvFeedingData.InvokeRequired)
                {
                    from.dgvFeedingData.Invoke(new Action<List<object>>(from.ShowFeedingData), list);
                }
                else
                {
                    if (list != null && list.Count > 0)
                    {
                        foreach (object m in list)
                        {
                            listTA.Add((IMData)m);
                        }
                        if (listTA.Count >= 500)
                        {
                            listTA.Clear();
                            GC.Collect();
                        }
                    }
                    // 设置最新行为活动行 (当前行)
                    from.dgvFeedingData.CurrentCell = from.dgvFeedingData.Rows[from.dgvFeedingData.Rows.Count - 1].Cells[0];
                }
            }
            catch (Exception ex)
            {
                //ShowLog("显示上料出现" + ex.Message, 1);
            }
        }
        #endregion

        /// <summary>
        /// 初始化入站表格
        /// </summary>
        /// <param name="from"></param>
        public static void InitDgvFeedingData(this DataGridView dataGridView)
        {
            dataGridView.SetColumns<IMData>();
        }

        /// <summary>
        /// 初始化出站表格
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void InitDgvBlankingData(this DataGridView dataGridView)
        {
            dataGridView.SetColumns<OUData>();
        }

        /// <summary>
        /// 初始化钢壳表格
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void InitDgvSteelShellData(this DataGridView dataGridView)
        {
            dataGridView.SetColumns<CData>();
        }

        #region 显示下料数据
        /// <summary>
        /// 显示下料数据
        /// </summary>
        /// <param name="list"></param>
        public static void ShowBankdingData(this FrmDataMonitor from, List<object> list)
        {
            try
            {
                if (from.dgvBlankingData.InvokeRequired)
                {
                    from.dgvBlankingData.Invoke(new Action<List<object>>(from.ShowBankdingData), list);
                }
                else
                {
                    //绑定数据到委托数据
                    if (list != null && list.Count > 0)
                    {
                        foreach (object m in list)
                        {
                            listTB.Add((OUData)m);
                        }
                        if (listTB.Count >= 500)
                        {
                            listTB.Clear();
                            GC.Collect();
                        }
                    }
                    int index = from.dgvBlankingData.Rows.Count - 1;
                    //this.dgvBlankingData.FirstDisplayedScrollingRowIndex = index;

                    // 设置最新行为活动行 (当前行)
                    from.dgvBlankingData.CurrentCell = from.dgvBlankingData.Rows[index].Cells[1];
                    from.dgvBlankingData.BeginEdit(true);

                }
            }
            catch (Exception ex)
            {
                //ShowLog("显示上料出现" + ex.Message, 1);
            }
        }

        #endregion

        public static void InitFrmDataMonitor(this FrmDataMonitor from)
        {
            from.InitDiolameFrm();
        }

        public static void setColumn(this DataGridView dataGridView, string HeaderText, string DataPropertyName, bool isFill = true)
        {
            dataGridView.Columns.Add(dataGridView.newColumn(HeaderText, DataPropertyName, isFill));
        }

        public static DataGridViewTextBoxColumn newColumn(this DataGridView dataGridView, string HeaderText, string DataPropertyName, bool isFill)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.HeaderText = HeaderText;
            column.DataPropertyName = DataPropertyName;
            if (isFill)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; ;
            }
            else
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }
            return column;
        }

        /// <summary>
        /// 获取dgv计算宽度
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        private static int GetCalculatedWidth(this DataGridView dataGridView, string text)
        {
            Font font = dataGridView.DefaultCellStyle.Font; // 获取当前字体
            SizeF textSize = new SizeF(0, 0);
            textSize = TextRenderer.MeasureText(text, font);
            return (int)Math.Ceiling(textSize.Width) + 10; // 计算宽度并考虑额外的边距(padding)
        }

        /// <summary>
        /// 设置表头单元格宽度
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <param name="HeaderText"></param>
        /// <param name="DataPropertyName"></param>
        /// <param name="name"></param>
        /// <param name="Width"></param>
        public static void SetColumnAutoWidth(this DataGridView dataGridView, string HeaderText, string DataPropertyName, string name, int Width = 20)
        {

            DataGridViewTextBoxColumn column = dataGridView.newColumn(HeaderText, DataPropertyName, false);
            column.Name = name;

            int calculatedWidth = dataGridView.GetCalculatedWidth(HeaderText);
            column.Width = Math.Max(calculatedWidth, Width); // 添加一个最小宽度限制
            dataGridView.Columns.Add(column);
        }


        public static void SetColumn(this DataGridView dataGridView, string HeaderText, string DataPropertyName, int Width)
        {

            DataGridViewTextBoxColumn column = dataGridView.newColumn(HeaderText, DataPropertyName, false);
            column.Width = Width;
            dataGridView.Columns.Add(column);

        }


    }
}
